package com.a100.equipment.controller;


import com.a100.equipment.entity.EquCategory;
import com.a100.equipment.entity.vo.CreateCategoryVo;
import com.a100.equipment.entity.vo.PageCategoryVo;
import com.a100.equipment.entity.vo.UpdateCategoryVo;
import com.a100.equipment.service.EquCategoryService;
import com.a100.utils.utils.R;
import com.a100.utils.utils.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 设备分类 前端控制器
 * </p>
 *
 * @author index
 * @since 2021-12-02
 */
@Api(description="设备分类前端控制器")
@RestController
@RequestMapping("/equipment/equ-category")
public class EquCategoryController {
    @Autowired
    private EquCategoryService equCategoryService;

    @ApiOperation(value = "新建设备类别")
    @PostMapping("/")
    public R saveEquCategoryByVo(@ApiParam(name = "createCategoryVo", value = "新增设备档案类", required = true)
                                   @RequestBody CreateCategoryVo createCategoryVo){
        //调用逻辑层完成新增功能
        boolean b=equCategoryService.saveEquCategoryByVo(createCategoryVo);
        //判断新增完成与否
        if(b){
            //新增成功
            return R.ok().code(ResultCode.SUCCESS.code()).success(true).message("新增设备成功");
        }
        return R.ok().message("新增设备失败");
    }
    @ApiOperation(value = "修改设备类别")
    @PutMapping("/")
    public R updateEquCategoryByVo(@ApiParam(name = "updateCategoryVo", value = "修改设备档案类", required = true)
                                 @RequestBody UpdateCategoryVo updateCategoryVo){
        //调用逻辑层完成新增功能
        boolean b=equCategoryService.updateEquCategoryByVo(updateCategoryVo);
        //判断新增完成与否
        if(b){
            //新增成功
            return R.ok().code(ResultCode.SUCCESS.code()).success(true).message("修改设备成功");
        }
        return R.ok().message("修改设备失败");
    }
    @ApiOperation(value = "条件分页查询所有分类")
    @PostMapping("/{current}/{pagesize}")
    public R listPageByQuery(@ApiParam(name = "current",value = "当前页",required = true)
                             @PathVariable("current")Integer current,
                             @ApiParam(name = "pagesize",value = "每页数量",required = true)
                             @PathVariable("pagesize")Integer pagesize,
                             @ApiParam(name = "QueryPlayersVo",value = "条件")
                             @RequestBody PageCategoryVo pageCategoryVo){
        //获得分页组件并返回结果
        HashMap<String, Object> hashMap=equCategoryService.listPageByQuery(current,pagesize,pageCategoryVo);
        if(StringUtils.isEmpty(hashMap)){
            return R.error();
        }
        //返回集合给前端
        return  R.ok().message("查询数据成功").code(ResultCode.SUCCESS.code()).success(true).data(hashMap);
    }
    @ApiOperation(value = "根据id查询类型")
    @GetMapping("/{equCategoryId}")
    public R chooseEquCategoryById(@ApiParam(name = "equCategoryId", value = "类型的id", required = true)
                                    @PathVariable("equCategoryId")String equCategoryId){
        EquCategory equCategory=equCategoryService.chooseEquCategoryById(equCategoryId);
        if(equCategory==null){
            return R.error().message("查询失败");
        }
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("equCategory",equCategory);
        //返回集合给前端
        return R.ok().data(hashMap).code(ResultCode.SUCCESS.code()).success(true).message("查询成功");
    }
    @ApiOperation(value = "查询全部类型")
    @GetMapping("/")
    public R chooseAllEquCategory(){
        List <EquCategory> equCategoryList=equCategoryService.chooseAllEquCategory();
        if(equCategoryList==null){
            return R.error().message("查询失败");
        }
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("equCategoryList",equCategoryList);
        //返回集合给前端
        return R.ok().data(hashMap).code(ResultCode.SUCCESS.code()).success(true).message("查询成功");
    }
    //todo 先删除所有设备再删除设备类型
}

